<?php
/**
 * Created by PhpStorm.
 * User: wzy12
 * Date: 2018/10/12
 * Time: 11:30
 */

namespace app\admin\controller;


class Article extends Base
{
    /**
     * 文章分类
     */
    public function cate(){
        $list = model('ArticleCat') -> paginate(10);

        $this -> assign('list', $list);
        return $this -> fetch();
    }

    public function cate_form(){
        if($this -> request -> isAjax()){
            $data = input('post.');

            $result = model('ArticleCat') -> operation($data);
            if(!$result['code']){
                return $this -> error($result['msg'], '', ['token' => $this -> request -> token()]);
            }else{
                return $this -> success($result['msg'], url('article/cate'), ['token' => $this -> request -> token()]);
            }
        }else{
            $id = input('id', 0);

            $res = null;
            if($id){
                $res = model('ArticleCat') -> find($id);
            }

            $this -> assign('id', $id);
            $this -> assign('res', $res);
            return $this -> fetch();
        }
    }

    public function cate_del(){
        if($this -> request -> isAjax()){
            $data = input('post.');

            $res = model('ArticleCat') -> del($data);
            if($res['code']){
                return $this -> success($res['msg'], url('Article/cate'), ['token' => $this -> request -> token()]);
            }else{
                return $this -> error($res['msg'], '', ['token' => $this -> request -> token()]);
            }
        }else{
            return $this -> error('非法请求', url('index/index'));
        }
    }

    /**
     * 文章列表
     */
    public function article(){
        $search_key = input();
        unset($search_key['/admin/article/article_html']);

        $arr = model('article') -> resList($search_key);
        unset($search_key['page']);
        if(!$search_key){
            $search_key = null;
        }

        $this -> assign('search_key', $search_key);
        $this -> assign('list', $arr['list']);
        $this -> assign('article_cat', $arr['article_cat']);
        return $this -> fetch();
    }

    public function article_form(){
        if($this -> request -> isAjax()){
            $data = input('post.');

            $result = model('Article') -> operation($data);
            if(!$result['code']){
                return $this -> error($result['msg'], '', ['token' => $this -> request -> token()]);
            }else{
                return $this -> success($result['msg'], url('article/article'), ['token' => $this -> request -> token()]);
            }
        }else{
            $id = input('id', 0);

            $res = null;
            if($id){
                $res = model('Article') -> find($id);
            }
            $cate = model('ArticleCat') -> select();

            $this -> assign('id', $id);
            $this -> assign('res', $res);
            $this -> assign('cate', $cate);
            return $this -> fetch();
        }
    }

    public function article_del(){
        if($this -> request -> isAjax()){
            $data = input('post.');

            $res = model('Article') -> del($data);
            if($res['code']){
                return $this -> success($res['msg'], url('article/article'), ['token' => $this -> request -> token()]);
            }else{
                return $this -> error($res['msg'], '', ['token' => $this -> request -> token()]);
            }
        }else{
            return $this -> error('非法请求', url('index/index'));
        }
    }

    /**
     * 文章内容图片上传
     */
    public function article_upload(){
        $file = input('file.img');
        $num = 1;

        if(!$file){
            $num = 0;
            $file = input('file.file');
        }

        $msg = $this -> upload($file);
        if(!$msg['code']){
            return $this -> error($msg['msg']);
        }else{
            return echoArr($num, $msg['msg'], ['img' => $msg['data']['img'], 'src' => config('imgRoute') . $msg['data']['img']]);
        }
    }

    /**
     * 分类排序
     */
    public function cateSort(){
        if($this -> request -> isAjax()){
            $data = input('post.');
            $result = model('ArticleCat') -> editSort($data);

            if($result['code']){
                return $this -> success($result['msg'], url('article/cate'), ['token' => $this -> request -> token()]);
            }else{
                return $this -> error($result['msg'], '', ['token' => $this -> request -> token()]);
            }
        }else{
            return $this -> error('非法请求');
        }
    }

    /**
     * 文章状态
     */
    public function articleStatus(){
        if($this -> request -> isAjax()){
            $data = input('post.');

            $result = model('Article') -> editStatus($data);

            if($result['code']){
                return $this -> success($result['msg'], url('article/article'), ['status' => $data['is_show']]);
            }else{
                return $this -> error($result['msg']);
            }
        }else{
            return $this -> error('非法请求');
        }
    }
}